<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

require_once('functions.php');

  if(!isset($_GET['a'])) {
    return;
  }
  
  
  $db = getConnection();

  $barcode = $_GET['a'];


  $actie = "";
  $actieBarcode = 0;
  $create = true;
  $ip = $_SERVER["REMOTE_ADDR"];
  foreach ($db->query("select * from sessie where ip='$ip' and ts > (unix_timestamp() - 60)") as $row) {
     $actie = $row['actie'];
     $actieBarcode = $row['barcode'];
     $create = false;
  }

  if ($create) {
    $db->exec("INSERT INTO sessie(ip, actie, barcode) VALUES ('$ip', '', 0) ON DUPLICATE KEY UPDATE actie='', barcode=0;");
  }



  if (($barcode > 1000) && ($barcode < 1500)) { // bandje kinderen
    if ($actie == 'gf') {
        $sql = "UPDATE persoon SET polsband='$barcode' WHERE barcode='$actieBarcode'";
        error_log("kopel ".$sql."\n", 3, "/tmp/php.log"); 
        $db->exec($sql);
        $db->exec("UPDATE sessie SET actie='', barcode=0, ts=unix_timestamp() WHERE ip='$ip'");
        echo "done";
    } else if ($actie == 'groep') { 
      foreach ($db->query("select * from groep where barcode=$actieBarcode") as $row) {
        $groepId = $row['id'];
        $kleur = $row['kleur'];
      }
        
      $sql = "select * from persoon where polsband=$barcode limit 1";
      error_log("kopel ".$sql."\n", 3, "/tmp/php.log"); 
      $result = $db->query($sql);
      if($result->rowCount() > 0 && !empty($result)) {
        foreach ($result as $row) {
          $sql = "UPDATE persoon SET groepId='$groepId' WHERE polsband='$barcode'";
          error_log("kopel ".$sql."\n", 3, "/tmp/php.log"); 
          $db->exec($sql);
          
          printf("%-16.16s%-16.16s", $kleur, ($row['roepnaam'] . " " .$row['achternaam']));
        }
      } else {
         printf("%-16.16s%-16.16s", $kleur, ("Onbekend " . $barcode));
      } 
    } else {
      
      $sql = "SELECT * FROM persoon WHERE polsband='$barcode' limit 1";
      $result = $db->query($sql);
      if($result->rowCount() > 0 && !empty($result)) {
        foreach ($db->query($sql) as $row) {     
          $tz  = new DateTimeZone('Europe/Brussels');
          $age = DateTime::createFromFormat('Y-m-d', $row['geboortedatum'], $tz)->diff(new DateTime('now', $tz))->y;
          printf("%-14.14s%2.2s%-16.16s", $row['roepnaam'], $age, $row['achternaam']);
        }
	$sql  = "SELECT * FROM presentie WHERE Polsband='$barcode' LIMIT 1";
  	error_log("presentie ".$barcode."\n", 3, "/tmp/php.log");
      	$result = $db->query($sql);
      	if($result->rowCount() > 0 && !empty($result)) {
          foreach ($db->query($sql) as $row) {
            $db->exec("UPDATE presentie SET ".getDay()."=1 WHERE Polsband='$barcode'");
          }
        } else {
          $db->exec("INSERT INTO presentie (polsband, ".getDay().") VALUES ($barcode, 1)");
	}
      } else {
          echo "bandje " . $barcode;
      }
    }
  } else if (($barcode > 1500) && ($barcode < 1650)) {
    echo "Leiding, Jij komt volgend jaar";
  } else if ($barcode > 1000000000000) {
    formulier($db, $barcode);
  } else if (($barcode >= 10000) && ($barcode < 10100)) {
    
    foreach ($db->query("select * from groep where barcode=$barcode") as $row) {
      echo $row['kleur'];
    }
    $ip = $_SERVER["REMOTE_ADDR"];
    $db->exec("UPDATE sessie SET actie='groep', barcode=$barcode, ts=unix_timestamp() WHERE ip='$ip'");

  } else {
    printf("%-16.16s%-16.16s", "Barcode onbekend", $barcode);
  }
  
  
        
        
  // close the database connection
  closeConnection($db);




function formulier($db, $barcode) {

  $sql = "SELECT * FROM persoon WHERE barcode=$barcode limit 1";
  $result = $db->query($sql);
  if($result->rowCount() > 0 && !empty($result)) {
        
    foreach ($db->query($sql) as $row) {
      list($year, $month, $day) = preg_split('/[\-]+/', $row['geboortedatum']);
     // printf("%02d-%02d-%02d ", intval($day), intval($month), intval($year) % 100);
      
      
      $tz  = new DateTimeZone('Europe/Brussels');
      $age = DateTime::createFromFormat('Y-m-d', $row['geboortedatum'], $tz)->diff(new DateTime('now', $tz))->y;
      
      // print $row['roepnaam'] . " " .$row['achternaam'] . " " . $age;
      printf("%-14.14s%2.2s%-16.16s", $row['roepnaam'], $age, $row['achternaam']);
     // print $_SERVER["REMOTE_ADDR"];
     
      $ip = $_SERVER["REMOTE_ADDR"];
      $db->exec("UPDATE sessie SET actie='gf', barcode=$barcode, ts=unix_timestamp() WHERE ip='$ip'");
     
    }
  } else {
    printf("%-16.16s%-16.16s", "Barcode onbekend", $barcode);
  }
  
  
}



?>
